package ph.com.gs3.formalistics.view.document.specialfields;

import java.text.ParseException;
import java.util.Date;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.global.constants.Constants;
import ph.com.gs3.formalistics.global.utilities.DateUtilities;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData;
import ph.com.gs3.formalistics.view.document.abstractfields.TextTypeFieldView;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

@SuppressLint("ViewConstructor")
public class DateTimePickerField extends TextTypeFieldView {

	public static final String TAG = DateTimePickerField.class.getSimpleName();

	private TextView tvLabel;
	private Button bSelectDateTime;

	private PickerType pickerType;

	private DateTimePickerListener listener;

	public static enum PickerType {
		DATE, TIME, DATETIME
	}

	public DateTimePickerField(Context context, FormFieldData formField, PickerType pickerType) {
		super(context, formField);

		this.pickerType = pickerType;

		createView();

		// Set data
		if (pickerType == PickerType.DATETIME || pickerType == PickerType.TIME) {
			bSelectDateTime.setText("Select");
		}

		setLabel(formField.getLabel());
		setPlaceHolder(formField.getPlaceHolder());
		setValue(formField.getValue());
	}

	protected void createView() {
		LayoutInflater.from(getContext()).inflate(R.layout.field_date_time_picker, this, true);

		tvLabel = (TextView) findViewById(R.id.FldDTR_tvLabel);
		setEditTextValueField((EditText) findViewById(R.id.FldDTR_etDateText));
		bSelectDateTime = (Button) findViewById(R.id.FldDTR_bSelectDateTime);

		tvLabel.setTypeface(Constants.getApplicationInstance().getGlobalTypeface());
		getEditTextValueField().setTypeface(Constants.getApplicationInstance().getGlobalTypeface());
		bSelectDateTime.setTypeface(Constants.getApplicationInstance().getGlobalTypeface());

		getEditTextValueField().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPickerView();
			}

		});

		bSelectDateTime.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showPickerView();
			}
		});

		this.setTag(fieldId + "_container");
		getEditTextValueField().setTag(fieldId);

		// TODO for testing only, remove later
		bSelectDateTime.setVisibility(View.GONE);

	}

	private void showPickerView() {

		if (listener != null) {
			listener.onOpenPickerViewCommand(this, pickerType, getValue());
		}

	}

	// =====================================================================
	// {{ Getters & Setters

	@Override
	public void setEnabled(boolean enabled) {

		getEditTextValueField().setEnabled(enabled);
		getEditTextValueField().setClickable(enabled);

	}

	public String getLabel() {
		return tvLabel.getText().toString();
	}

	public void setLabel(String label) {
		tvLabel.setText(label);
	}

	public Date getDateValue() {

		String rawStringValue = getEditTextValueField().getText().toString();

		if (rawStringValue.trim() == "") {
			rawStringValue = "0000-00-00 00:00:00";
		}

		try {

			switch (pickerType) {
				case DATE:
					return DateUtilities.DEFAULT_DISPLAY_DATE_ONLY_FORMAT.parse(rawStringValue);
				case DATETIME:
					return DateUtilities.DEFAULT_DISPLAY_DATE_TIME_FORMAT.parse(rawStringValue);
				case TIME:
					return DateUtilities.DEFAULT_DISPLAY_TIME_ONLY_FORMAT.parse(rawStringValue);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return null;

	}

	public String getValue() {

		Date dateValue = getDateValue();
		String formattedValue;

		switch (pickerType) {
			case DATE:
				if (dateValue == null) {
					formattedValue = "0000-00-00";
				} else {
					formattedValue = DateUtilities.SERVER_DATE_FORMAT.format(dateValue);
				}
				break;
			case TIME:
				if (dateValue == null) {
					formattedValue = "00:00:00";
				} else {
					formattedValue = DateUtilities.SERVER_TIME_FORMAT.format(dateValue);
				}
				break;
			case DATETIME:
			default:
				if (dateValue == null) {
					formattedValue = "0000-00-00 00:00:00";
				} else {
					formattedValue = DateUtilities.SERVER_DATE_TIME_FORMAT.format(dateValue);
				}
				break;
		}

		return formattedValue;
	}

	public void setValue(String value) {
		getEditTextValueField().setText(value);
	}

	public void setPlaceHolder(String placeholder) {

		String hint = "";

		if (placeholder == null || placeholder.isEmpty()) {
			switch (pickerType) {
				case DATE:
					hint = "MMM/dd/yyy";
					break;
				case TIME:
					hint = "HH:mm (AM/PM)";
					break;
				case DATETIME:
					hint = "MMM/dd/yyy HH:mm (AM/PM)";
					break;

			}
		} else {
			hint = placeholder;
		}

		getEditTextValueField().setHint(hint);
	}

	public DateTimePickerListener getListener() {
		return listener;
	}

	public void setListener(DateTimePickerListener listener) {
		this.listener = listener;
	}

	// }}

	// =====================================================================
	// {{ Interface(s)

	public static interface DateTimePickerListener {

		public void onOpenPickerViewCommand(DateTimePickerField source, PickerType pickerType,
		        String currentFieldValue);

	}

	// }}

}
